﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using ZoneFiveSoftware;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Visuals.Fitness;

namespace PolarImport.DeviceImporter
{
    public partial class DeviceConfiguration : Form
    {
        internal DeviceConfigurationInfo ConfigInfo
        {
            get 
            { 
                return configInfo; 
            }
            set
            {
                configInfo = value;
                
                this.chkBasicValues.Checked = value.WriteBasicValues;
                this.chkLapData.Checked  = value.WriteLapData;
                this.chkTimeBasedValues.Checked = value.WriteTimeBasedValues;
                this.chkDetail.Checked = value.WriteDetailedInfos;
                this.cbDownloadConnection.SelectedItem = value.DownloadConnection;
                this.cbUploadConnection.SelectedItem = value.UploadConnection;
#if(DEBUG)
                Plugin.AddDebugInfo("configInfo gesetzt:");
                Plugin.AddDebugInfo(value.ToString());
#endif
                ChangeEnabledState();
                ChangeAdditionalInputs(value.DownloadConnection);
                if (configInfo.ComPort < 1)
                {
                    configInfo.ComPort = 1;
                }
                else if (configInfo.ComPort > 9)
                {
                    configInfo.ComPort = 9;
                }
                this.numericUpDown1.Value = configInfo.ComPort;
                
                if (configInfo.Amplification < 1)
                {
                    configInfo.Amplification = 1;
                }
                else if (configInfo.Amplification > 10)
                {
                    configInfo.Amplification = 10;
                }
                this.nudAmp.Value = configInfo.Amplification;

                if (configInfo.SamplingFrequency < 0)
                {
                    configInfo.SamplingFrequency = 0;
                }
                else if (configInfo.SamplingFrequency > 1)
                {
                    configInfo.SamplingFrequency = 1;
                }
                this.cbFreq.SelectedIndex = configInfo.SamplingFrequency;
            }
        }
        private DeviceConfigurationInfo configInfo;
        private Settings.PolarDevice device;

        private bool ChangeEnabledState()
        {
            if (device != null)
            {
                this.chkBasicValues.Enabled = device.BasicValues;
                if (this.chkBasicValues.Enabled == false)
                    this.chkBasicValues.Checked = false;
                this.chkLapData.Enabled = device.LapData ;
                if (this.chkLapData.Enabled == false)
                    this.chkLapData.Checked = false;
                this.chkTimeBasedValues.Enabled = device.TimeBasedValues ;
                if (this.chkTimeBasedValues.Enabled == false)
                    this.chkTimeBasedValues.Checked = false;
                return true;
            }
            return false;
        }

        public DeviceConfiguration(Settings.PolarDevice Device)
        {
            InitializeComponent();
            device = Device;
            configInfo = new DeviceConfigurationInfo();
            this.btnOK.Text = CommonResources.Text.ActionOk;
            this.btnCancel.Text = CommonResources.Text.ActionCancel;
            foreach (DeviceConnection Connection in device.UploadConnections )
                cbUploadConnection.Items.Add(Connection);
            foreach (DeviceConnection Connection in device.DownloadConnections)
                cbDownloadConnection.Items.Add(Connection);

            // Localize all texts
            this.Text = string.Format(Properties.Resources.PolarImport_Settings_ImportConfiguration, device.Modells);
            this.lblDownload.Text = Properties.Resources.PolarImport_Settings_DownloadConnection;
            this.lblUpload.Text = Properties.Resources.PolarImport_Settings_UploadConnection;
            this.chkBasicValues.Text = Properties.Resources.PolarImport_Settings_WriteBasicValues;
            this.chkTimeBasedValues.Text = Properties.Resources.PolarImport_Settingsl_WriteTimeBasedValues;
            this.chkLapData.Text = Properties.Resources.PolarImport_Settings_WriteLapData;
            this.chkDetail.Text = Properties.Resources.PolarImport_Settings_WriteDetailedInfos;

            ChangeEnabledState();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            configInfo.WriteBasicValues = chkBasicValues.Checked;
            configInfo.WriteDetailedInfos = chkDetail.Checked;
            configInfo.WriteLapData = chkLapData.Checked;
            configInfo.WriteTimeBasedValues = chkTimeBasedValues.Checked;
            configInfo.DownloadConnection = (DeviceConnection)Enum.Parse(typeof(DeviceConnection), cbDownloadConnection.SelectedItem.ToString());
            configInfo.ComPort = (int)numericUpDown1.Value;
            configInfo.Amplification = (int)nudAmp.Value;
            configInfo.SamplingFrequency = cbFreq.SelectedIndex;
            configInfo.UploadConnection = (DeviceConnection)Enum.Parse(typeof(DeviceConnection), cbUploadConnection.SelectedItem.ToString());
            #if (DEBUG)
            Plugin.AddDebugInfo("Config-Dialog mit OK verlassen. Neuer config String:");
            Plugin.AddDebugInfo(configInfo.ToString());
            #endif
            base.DialogResult = this.btnOK.DialogResult;
            base.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            #if (DEBUG)
            Plugin.AddDebugInfo("Config-Dialog mit CANCEL verlassen. Neuer config String:");
            #endif
            base.DialogResult = this.btnCancel.DialogResult;
            base.Close();
        }

        private void DeviceConfiguration_Shown(object sender, EventArgs e)
        {
            System.Resources.ResourceManager resourceManager = Properties.Resources.ResourceManager;
            pictureBox1.Image = (Image)resourceManager.GetObject("Img_Polar_" + device.Monitor.ToString());
           
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the cbDownloadConnection control.
        /// </summary>
        /// <remarks>Ändert den Enabledstate vom Com-Port.</remarks>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbDownloadConnection_SelectedIndexChanged(object sender, EventArgs e)
        {
            DeviceConnection selectedDownloadConnection = (DeviceConnection)Enum.Parse(typeof(DeviceConnection), cbDownloadConnection.SelectedItem.ToString());

            // Nur bei ausgewählter DownloadConnection IRCOM soll
            // Die Eingabe des Ports möglich sein.
            ChangeAdditionalInputs(selectedDownloadConnection);
        }

        private void ChangeAdditionalInputs(DeviceConnection selectedDownloadConnection)
        {
            if (selectedDownloadConnection == DeviceConnection.IrCOM)
            {
                this.label2.Enabled = true;
                this.numericUpDown1.Enabled = true;
                this.lblAmp.Enabled = false;
                this.cbFreq.Enabled = false;
                this.lblFrq.Enabled = false;
                this.nudAmp.Enabled = false;
            }
            // Oder bei SonicLink die Sound Einstellungen
            else if (selectedDownloadConnection == DeviceConnection.SonicLink)
            {
                this.label2.Enabled = false;
                this.numericUpDown1.Enabled = false;
                this.lblAmp.Enabled = true;
                this.cbFreq.Enabled = true;
                this.lblFrq.Enabled = true;
                this.nudAmp.Enabled = true;
            }
            else if (selectedDownloadConnection == DeviceConnection.SonicLink)
            {
                this.label2.Enabled = false;
                this.numericUpDown1.Enabled = false;
                this.lblAmp.Enabled = false;
                this.cbFreq.Enabled = false;
                this.lblFrq.Enabled = false;
                this.nudAmp.Enabled = false;
            }
        }
    }
}
